package net.asrsmid.connection.rs;

import java.net.MalformedURLException;

import javax.xml.ws.BindingProvider;

import net.asrsmid.config.RsApplication;

import org.apache.commons.pool.PoolableObjectFactory;

import com.microsoft.rs2005.ReportExecutionServiceSoap;

/**
 * @author Chen WANG
 * @version 1.0
 * @created 05-mai-2011 11:53:13
 */
public class RsWebserviceProxyFactory implements PoolableObjectFactory {
	private RsApplication rsApp;

	public RsWebserviceProxyFactory(RsApplication rsApp) {
		this.rsApp = rsApp;
	}
	
	public RsWebserviceProxy makeObject() {
		RsWebserviceProxy proxy = null;
		try {
			proxy = new RsWebserviceProxy(rsApp);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	public void activateObject(Object arg0) throws Exception {	
	}

	public void destroyObject(Object arg0) throws Exception {
		arg0 = null;
	}

	public void passivateObject(Object arg0) throws Exception {
		RsWebserviceProxy rsWsproxy = (RsWebserviceProxy) arg0;
		rsWsproxy.resetAuthentication();
		/*ReportExecutionServiceSoap ress = rsWsproxy.getReportExecutionServiceSoap();
		
		((BindingProvider)ress).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, null);
		((BindingProvider)ress).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, null);*/
	}

	public boolean validateObject(Object arg0) {
		RsWebserviceProxy rsWsproxy = (RsWebserviceProxy) arg0;
		ReportExecutionServiceSoap ress = rsWsproxy.unwrap();
		
		if (ress != null && ((BindingProvider) rsWsproxy).getRequestContext() != null)
			return true;
		else
			return false;
	}
}